package org.wkg.lesson05;

import java.util.Scanner;

public class Demo_502 {
    // 定义退出标识
    static volatile int flag = 0;

    public static void main(String[] args) {

        // 创建执行任务的线程
        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "线程启动.");
            while (flag == 0) {
                // 不停的循环. 处理任务

            }
            System.out.println(Thread.currentThread().getName() + "线程退出.");
        }, "t1");
        // 启动线程
        t1.start();

        // 输入停止标识
        Thread t2 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "线程启动.");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个非零的整数：");
            flag = scanner.nextInt();
            System.out.println(Thread.currentThread().getName() + "线程退出.");
        }, "t2");
        // 线程
        t2.start();

    }
}